﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Sobiens.SPCamlStudio.Common.Interfaces;
using Sobiens.SPCamlStudio.Entities.Interfaces;
using System.Collections;
using Sobiens.SPCamlStudio.Entities;
using System.IO;
using Sobiens.SPCamlStudio.Common;
using Sobiens.SPCamlStudio.Entities.Settings;
using Sobiens.SPCamlStudio.UI.Controls;
using Sobiens.SPCamlStudio.Entities.SharePoint;
using System.Data;
using Sobiens.SPCamlStudio.Entities.Data;
using System.Windows.Threading;
using System.Threading;
using System.Windows;
using System.Windows.Data;

namespace Sobiens.SPCamlStudio.UI
{
    public class SPCamlStudioApplicationManager : ApplicationManager
    {
        //public AppConfiguration Configuration = new AppConfiguration();

        public SPCamlStudioApplicationManager(ISPCamlStudio spCamlStudio)
        {
            this.SPCamlStudio = spCamlStudio;
        }

        public override void SetStatusBar(string text, int percentage)
        {
            //this.ConnectorExplorer.StatusBar.SetStatusBar(text, percentage);
        }
        public override IQueryPanel AddNewQueryPanel(Folder folder, ISiteSetting siteSetting)
        {
            return this.SPCamlStudio.QueriesPanel.AddNewQueryPanel(folder, siteSetting);
        }

        public override ISiteSetting GetSiteSetting(Guid siteSettingID)
        {
            return Configuration.SiteSettings[siteSettingID];
        }

        public override void Initialize()
        {
            this.IsInitialized = true;
            ApplicationContext.Current.Configuration = ConfigurationManager.GetInstance().LoadConfiguration();
            this.SPCamlStudio.QueryDesignerToolbar.ValidateButtonEnabilities();
            this.SPCamlStudio.ServerObjectExplorer.Initialize();
            //            this.RefreshControlsFromConfiguration();
        }

        public override void RefreshControlsFromConfiguration()
        {
            //this.SPCamlStudio.RefreshControls();
        }

        public override Folder GetRootFolder(ISiteSetting siteSetting)
        {
            IServiceManager serviceManager = ServiceManagerFactory.GetServiceManager(siteSetting.SiteSettingType);
            return serviceManager.GetRootFolder(siteSetting);
        }

        public override Folder GetParentFolder(ISiteSetting siteSetting, Folder folder)
        {
            IServiceManager serviceManager = ServiceManagerFactory.GetServiceManager(siteSetting.SiteSettingType);
            return serviceManager.GetParentFolder(siteSetting, folder);
        }

        public override List<Folder> GetSubFolders(ISiteSetting siteSetting, Folder folder, int[] includedFolderTypes)
        {
            IServiceManager serviceManager = ServiceManagerFactory.GetServiceManager(siteSetting.SiteSettingType);
            return serviceManager.GetFolders(siteSetting, folder, includedFolderTypes);
        }

        public override List<IView> GetViews(ISiteSetting siteSetting, Folder folder)
        {
            IServiceManager serviceManager = ServiceManagerFactory.GetServiceManager(siteSetting.SiteSettingType);
            return serviceManager.GetViews(siteSetting, folder);
        }

        public override FieldCollection GetFields(ISiteSetting siteSetting, Folder folder)
        {
            IServiceManager serviceManager = ServiceManagerFactory.GetServiceManager(siteSetting.SiteSettingType);
            return serviceManager.GetFields(siteSetting, folder);
        }

        public override IView GetView(ISiteSetting siteSetting, Folder folder)
        {
            return null;
        }

        public override List<IItem> GetListItems(ISiteSetting siteSetting, Folder folder, IView view, string sortField, bool isAsc, int currentPageIndex, string currentListItemCollectionPositionNext, CamlFilters filters, bool isRecursive, out string listItemCollectionPositionNext, out int itemCount)
        {
            IServiceManager serviceManager = ServiceManagerFactory.GetServiceManager(siteSetting.SiteSettingType);
            return serviceManager.GetListItems(siteSetting, folder, view, sortField, isAsc, currentPageIndex, currentListItemCollectionPositionNext, filters, isRecursive, out listItemCollectionPositionNext, out itemCount);
        }

        public override List<IItem> GetListItems(ISiteSetting siteSetting, List<CamlOrderBy> orderBys, CamlFilters filters, List<CamlFieldRef> viewFields, CamlQueryOptions queryOptions, string webUrl, string listName, out string listItemCollectionPositionNext, out int itemCount)
        {
            IServiceManager serviceManager = ServiceManagerFactory.GetServiceManager(siteSetting.SiteSettingType);
            return serviceManager.GetListItems(siteSetting, orderBys, filters, viewFields, queryOptions, webUrl, listName, out listItemCollectionPositionNext, out itemCount);
        }

        public override void Save()
        {
            this.SPCamlStudio.QueriesPanel.Save();
        }

        public override void Load(string filename)
        {
            this.SPCamlStudio.QueriesPanel.Load(filename);
        }

    }
}
